//
//  InstallerSignForNewStcFormView.swift
//  ezyform
//
//  Created by Moennig Q on 6/5/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class InstallerSignForNewStcFormView: GreenNaviBarView {

    let signView = SignatureView()
    lazy var swhTakeSelfieTipLabel = ContentLabel(text: "By signing, you agree to this device (GreenDeal App) taking a photo of you while you sign off to verify your identity.",hexStr: Constant.ThemeColorHexStr)
    let signFullNameTitle = ContentLabel()
    let signFullNameLabel = ContentLabel(hexStr: "#292D32B2")
    let cecAccreditationNoTitle = ContentLabel()
    let cecAccreditationNoLabel = ContentLabel(hexStr: "#292D32B2")
    let signDateLabel = ContentLabel(hexStr: "#292D32B2")
    let selectWitnessView = SignToSelectWitnessView()
   
    override func setUpView() {
        let contentBackView = UIView()
        contentBackView.drawCorner(byRoundingCorners: [.topLeft,.topRight])
        self.addSubview(contentBackView)
        contentBackView.snp.makeConstraints { make in
            make.top.equalTo(topBackView.snp.bottom).offset(-20)
            make.left.right.bottom.equalToSuperview()
        }
        //infoview
        let signInfoView = UIView()
        contentBackView.addSubview(signInfoView)
        signInfoView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-30)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        //热水器没有协议，签名是需要自拍，所有要加上这句提醒
        if JobDetailModelHelper.shared.jobDetailModel.solar_water_heater {
            swhTakeSelfieTipLabel.numberOfLines = 0
            signInfoView.addSubview(swhTakeSelfieTipLabel)
            swhTakeSelfieTipLabel.snp.makeConstraints { (make) in
                make.top.equalToSuperview()
                make.left.right.equalToSuperview()
                make.height.greaterThanOrEqualTo(0)
            }
        }
        //sign full name
        signFullNameTitle.textAlignment = .left
        signFullNameTitle.numberOfLines = 0
        signInfoView.addSubview(signFullNameTitle)
        signFullNameTitle.snp.makeConstraints { (make) in
            if JobDetailModelHelper.shared.jobDetailModel.solar_water_heater {
                make.top.equalTo(swhTakeSelfieTipLabel.snp.bottom).offset(12)
            }else{
                make.top.equalToSuperview()
            }
            make.left.equalToSuperview()
            make.width.equalTo(180)
            make.height.greaterThanOrEqualTo(0)
        }
        signFullNameLabel.textAlignment = .right
        signInfoView.addSubview(signFullNameLabel)
        signFullNameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(signFullNameTitle.snp.right)
            make.right.equalToSuperview()
            make.centerY.equalTo(signFullNameTitle)
            make.height.greaterThanOrEqualTo(0)
        }
        //cec
        cecAccreditationNoTitle.textAlignment = .left
        signInfoView.addSubview(cecAccreditationNoTitle)
        cecAccreditationNoTitle.snp.makeConstraints { (make) in
            make.top.equalTo(signFullNameTitle.snp.bottom).offset(12)
            make.left.equalToSuperview()
            make.width.equalTo(signFullNameTitle)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        cecAccreditationNoLabel.textAlignment = .right
        signInfoView.addSubview(cecAccreditationNoLabel)
        cecAccreditationNoLabel.snp.makeConstraints { (make) in
            make.left.equalTo(cecAccreditationNoTitle.snp.right)
            make.right.equalToSuperview()
            make.centerY.equalTo(cecAccreditationNoTitle)
            make.height.greaterThanOrEqualTo(0)
        }
        //sign date
        let signDateTitle = ContentLabel()
        signDateTitle.text = R.string.localizable.signatureDate()
        signDateTitle.textAlignment = .left
        signInfoView.addSubview(signDateTitle)
        signDateTitle.snp.makeConstraints { (make) in
            make.top.equalTo(cecAccreditationNoTitle.snp.bottom).offset(12)
            make.left.equalToSuperview()
            make.width.equalTo(signFullNameTitle)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        signDateLabel.textAlignment = .right
        signInfoView.addSubview(signDateLabel)
        signDateLabel.snp.makeConstraints { (make) in
            make.left.equalTo(signDateTitle.snp.right)
            make.right.equalToSuperview()
            make.centerY.equalTo(signDateTitle)
            make.height.greaterThanOrEqualTo(0)
        }
        //select witness view
        signInfoView.addSubview(selectWitnessView)
        selectWitnessView.snp.makeConstraints { (make) in
            make.top.equalTo(signDateTitle.snp.bottom).offset(20)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview().offset(-10)
        }
        //sign view
        signView.layer.cornerRadius = Constant.BackViewCornerRadius
        signView.backgroundColor = .white
        signView.clipsToBounds = true
        signView.layer.borderWidth = 1
        signView.layer.borderColor = UIColor(hexStr: "#292D3233").cgColor
        contentBackView.addSubview(signView)
        signView.snp.makeConstraints { (make) in
            make.bottom.equalTo(signInfoView.snp.top).offset(-20)
            make.top.equalToSuperview().offset(20)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        
        let clearBtn = BaseButton()
        clearBtn.setTitle(R.string.localizable.clear(), for: UIControl.State.normal)
        clearBtn.setTitleColor(UIColor(hexStr: Constant.ThemeColorHexStr), for: UIControl.State.normal)
        clearBtn.setFont(R.font.interMedium(size: 14))
        contentBackView.addSubview(clearBtn)
        clearBtn.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(signView)
            make.height.equalTo(44)
            make.width.equalTo(60)
            
        }
        clearBtn.addTarget(self, action: #selector(clearSignature), for: UIControl.Event.touchUpInside)
    }
    func setData(_ name: String, accreditedName: String){
        let signTitle = "Signature of the " + APPConfig.loginInfo.currentSignatureType.rawValue + " : "
        self.signFullNameTitle.text = signTitle
        self.signFullNameLabel.text = name
        if APPConfig.loginInfo.installer {
            cecAccreditationNoTitle.text = R.string.localizable.cecAccreditationNo()
            self.cecAccreditationNoLabel.text = accreditedName
        }else if APPConfig.loginInfo.plumber {
            cecAccreditationNoTitle.text = "Plumber licence:"
            self.cecAccreditationNoLabel.text = APPConfig.loginInfo.plumber_licenses
        }else{
            cecAccreditationNoTitle.text = R.string.localizable.cecAccreditationNo()
            self.cecAccreditationNoLabel.text = accreditedName
        }
        //sign date
        let signDate = DateUtil().getDatetimeStr(Date(), format: "MMM dd, yyyy")
        self.signDateLabel.text = signDate
        
    }
    
    @objc func clearSignature(){
        self.signView.clearSignature()
    }
}
